package Control;

import javax.imageio.ImageIO;
import javax.swing.*;
import Database.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.sql.ResultSet;

public class LoginGUI extends JFrame implements ActionListener
{
	private JButton login, cancel;
	private JPanel logo, p1, p2;
	private JLabel logoPicture;
	private JTextField username;
	private JPasswordField password;
	private TableOperations to;
	ImageIcon icon = new ImageIcon("frameIcon.png");

	public LoginGUI() {
		setIconImage(icon.getImage());
		setTitle("MDL Login");
		setSize(300, 295);
		setLocationRelativeTo(null);
		setResizable(false);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setLayout(new GridLayout(3, 1));

		addWindowListener(new java.awt.event.WindowAdapter() {
			@Override
			public void windowClosing(java.awt.event.WindowEvent windowEvent) {
				to.close();
			}
		});

		GridBagLayout gridbagLayoutSettings = new GridBagLayout();
		GridBagConstraints gc = new GridBagConstraints();
		Insets insets = new Insets(5, 10, 15, 5);
		to = new TableOperations();

		username = new JTextField(25);
		password = new JPasswordField(25);
		BufferedImage myPicture = null;

		// Add the logo
		try {
			myPicture = ImageIO.read(new File("logo.png"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		logo = new JPanel();
		logoPicture = new JLabel(new ImageIcon(myPicture));
		logo.add(logoPicture);
		this.add(logo);

		// Panel 1
		p1 = new JPanel();
		p1.setLayout(gridbagLayoutSettings);

		gc.gridx = 0;
		gc.gridy = 1;
		gc.weighty = 0;
		gc.weightx = 0;
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		p1.add(new JLabel("Username"), gc);

		gc.gridx = 1;
		gc.gridy = 1;
		gc.weighty = 0;
		gc.weightx = 2;
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		p1.add(username, gc);

		gc.gridx = 0;
		gc.gridy = 3;
		gc.weighty = 0;
		gc.weightx = 0;
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		p1.add(new JLabel("Password"), gc);

		gc.gridx = 1;
		gc.gridy = 3;
		gc.weighty = 0;
		gc.weightx = 2;
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		p1.add(password, gc);

		this.add(p1, BorderLayout.CENTER);

		// Panel 2
		p2 = new JPanel();
		p2.setLayout(gridbagLayoutSettings);

		gc.gridx = 1;
		gc.gridy = 1;
		gc.weighty = 0;
		gc.weightx = 0;
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		login = new JButton("Login");
		login.addActionListener(this);
		login.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
				KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "Enter");
		Action action = new AbstractAction() {
			@Override
			public void actionPerformed(ActionEvent e) {
				login = (JButton) e.getSource();
				login();
			}
		};
		login.getActionMap().put("Enter", action);
		login.addActionListener(action);
		p2.add(login, gc);

		gc.gridx = 3;
		gc.gridy = 1;
		gc.weighty = 0;
		gc.weightx = 0;
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		cancel = new JButton("Cancel");
		cancel.addActionListener(this);
		p2.add(cancel, gc);

		this.add(p2, BorderLayout.SOUTH);
	}

	public void login() 
	{
		@SuppressWarnings("deprecation")
		boolean login = to.userLogin(username.getText(), password.getText());
		if (login == true) 
		{
			this.dispose();
			// Check if User is a Manager or a Sales Assistant
			boolean isManager = to.isManagerOrSA(username.getText());
			if (isManager == true) 
			{
				ManagerGUI mGUI = new ManagerGUI(to.getName(username.getText()), to);
				mGUI.setVisible(true);
				this.dispose();
			} 
			else 
			{
				SalesAssistantMain sam = new SalesAssistantMain(to.getSA(username.getText()), to);
				sam.setVisible(true);
				this.dispose();
			}
		} 
		else
		{
			password.setText("");
		}
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == cancel) {
			to.close();
			System.exit(0);
		}
	}

	public static void main(String[] args) {
		LoginGUI gui = new LoginGUI();
		gui.setVisible(true);
	}
}
